package ua.races.jsptag;

import ua.races.dao.RacesDaoImpl;
import org.apache.log4j.Logger;
import ua.races.vo.*;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class PreviousBetsTag extends TagSupport {

    private static final Logger log = Logger.getLogger(PreviousBetsTag.class.getName());

    private Client client;

    public void setClient(Client client) {
        this.client = client;
    }

    @Override
    public int doStartTag() throws JspException {

        List<Bet> bets = null;
        RacesDaoImpl dao;
        Race race = null;
        Horse horse[] = {null,null,null};
        BetType betType = null;


        dao = (RacesDaoImpl) pageContext.getServletContext().getAttribute("DAO");

        try {
            bets = dao.findAllClientBets(client);
        } catch (SQLException e) {
            log.error(e);
        }

        JspWriter w = pageContext.getOut();
        try {
            w.println("<table summary=\"\" border=\"1\" bgcolor=\"#add8e6\">");
            w.println("<thead bgcolor = \"#e9967a\" >");
            w.println("<tr >\n" +

                    // BET_VALUE  	BET_DATE  	RACE_ID  	BET_TYPE_ID  	FIRST_HORSE  	SECOND_HORSE
                    // THIRD_HORSE  	RESULT
                    "        <th > Bet Date </th >\n" +
                    "        <th > Race Date </th >\n" +
                    "        <th > Stadium </th >\n" +
                    "        <th > Bet Type </th >\n" +
                    "        <th > First horse </th >\n" +
                    "        <th > Second horse  </th >\n" +
                    "        <th > Third horse </th >\n" +
                    "        <th > Value </th >\n" +
                    "        <th > Result </th >\n" +
                    "        <th > Info </th >\n" +
                    "</tr >\n" +
                    "</thead >\n" +
                    "<tbody>");

            for (Bet b : bets) {

                try {
                    race = dao.findRaceById(b.getRaceId());
                    horse[0] = dao.findHorseById(b.getFirstHorse());
                    if (b.getSecondHorse()!=null){
                    horse[1] = dao.findHorseById(b.getSecondHorse());
                    }
                    if (b.getThirdHorse()!=null){
                    horse[2] = dao.findHorseById(b.getThirdHorse());
                    }

                    betType = dao.findBetTypeById(b.getBetTypeId());
                } catch (SQLException e) {
                    log.error(e);
                }

                w.println("<tr >\n" +
                        "        <td ><input type = \"hidden\"" +
                        "        name = \"raceId\"" +
                        "        value = \"" + race.getRaceId() + "\">" + b.getBetDate() +
                        "</td>");
                w.println("<td>" + race.getRaceDate() + "</td>");
                w.println("<td>" + race.getStadium() + "</td>");
                w.println("<td>" + betType.getBetType() + "</td>");
                w.println("<td>" + horse[0].getHorseName() + "</td>");
                if (horse[1] != null) {
                    w.println("<td>" + horse[1].getHorseName() + "</td>");
                } else {
                    w.println("<td bgcolor = \"#e99671\"></td>");
                }
                if (horse[2] != null) {
                    w.println("<td>" + horse[2].getHorseName() + "</td>");
                } else {
                    w.println("<td bgcolor = \"#e99671\"></td>");
                }
                w.println("<td>" + b.getBetValue() + "</td>");
                if (b.getResult() != null) {
                    w.println("<td>" + b.getResult() + "</td>");
                } else {
                    w.println("<td>Waiting for the results of the race</td>");
                }

                w.println("<td ><input type = \"submit\"" +
                        " value = \"view details\" / ></td >\n</tr>");

            }

            w.println("</tbody >\n" +
                    "        </table >");

        } catch (IOException
                e) {
            log.error(e);
        }

        return SKIP_BODY;

    }

    @Override
    public int doEndTag
            () throws JspException {
        return EVAL_PAGE;
    }
}
